﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using EFFC.Net.Data.Base;
using EFFC.Net.Interface;


namespace EFFC.Net.Parameters
{
    [Serializable]
    public class DBAP : ParametersStd
    {
        #region IParameters Members

        public DBAP()
        {
            this.AddPElement("SQL");
            this.AddPElement("SQL_Parameters", typeof(DBOParameterCollection));
            this.AddPElement("StoreProcureName");
            this.AddPElement("StoreProcure_IsReturnDataSet", typeof(bool));
            this.AddPElement("Table_Name");
        }

        public string SQL
        {
            get
            {
                return GetValue<string>("SQL");
            }
            set
            {
                this.SetValue("SQL", value);
            }
        }
        public DBOParameterCollection SQL_Parameters
        {
            get
            {
                return GetValue<DBOParameterCollection>("SQL_Parameters");
            }
            set
            {
                this.SetValue("SQL_Parameters", value);
            }
        }
        public string StoreProcureName
        {
            get
            {
                return GetValue<string>("StoreProcureName");
            }
            set
            {
                this.SetValue("StoreProcureName", value);
            }
        }
        public bool StoreProcure_IsReturnDataSet
        {
            get
            {
                return GetValue<bool>("StoreProcure_IsReturnDataSet");
            }
            set
            {
                this.SetValue("StoreProcure_IsReturnDataSet", value);
            }
        }
        public string Table_Name
        {
            get
            {
                return GetValue<string>("Table_Name");
            }
            set
            {
                this.SetValue("Table_Name", value);
            }
        }

        #endregion

        public void SetSQL_Parameters(ParametersStd p)
        {
            if (this.SQL != "")
            {
                RegexStd re = new RegexStd(@"(?<=@)[A-Za-z_]+\d*");
                if(re.IsMatch(this.SQL)){
                    this.SQL_Parameters = new DBOParameterCollection();
                }
                foreach (System.Text.RegularExpressions.Match m in re.Matches(this.SQL))
                {
                    this.SQL_Parameters.Add(m.ToString(), p.GetValue(m.ToString()));
                }
            }
        }

        public void SetSQL_Parameters(IModule p)
        {
            if (this.SQL != "")
            {
                RegexStd re = new RegexStd(@"(?<=@)[A-Za-z_]+\d*");
                if (re.IsMatch(this.SQL))
                {
                    this.SQL_Parameters = new DBOParameterCollection();
                }
                foreach (System.Text.RegularExpressions.Match m in re.Matches(this.SQL))
                {
                    if (p.ColumnNames.Contains(m.ToString()))
                    {
                        this.SQL_Parameters.Add(m.ToString(), p.GetValue(m.ToString()));
                    }
                }
            }
        }
    }
}
